package io.sage.modules.basics.controller;

import java.util.*;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.sage.common.utils.R;
import io.sage.modules.basics.entity.BGoodsType;
import io.sage.modules.basics.service.BGoodsTypeService;
import io.sage.modules.sys.entity.SysUserEntity;
import io.sage.modules.sys.shiro.ShiroUtils;

/**
 * 货物类型表
 *
 * @author xujh
 * @email jhang_mail@163.com
 * @date 2019-07-08 22:38:05
 */
@RestController
@RequestMapping("basics/bgoodstype")
public class BGoodsTypeController {
    @Autowired
    private BGoodsTypeService bGoodsTypeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("basics:bgoodstype:list")
    public R list(@RequestParam Map<String, Object> params){
    	Integer pageSize = 1;
		if (params != null && params.get("limit") != null) {
			pageSize = java.lang.Integer.parseInt(params.get("limit").toString());
		}
		Integer pageNum = 10;
		if (params != null && params.get("page") != null) {
			pageNum = java.lang.Integer.parseInt(params.get("page").toString());
		}

		HashMap<String, Object> paramMap = new HashMap<String, Object>();

		if(params.get("typeName")!=null){
			if(!"".equals(params.get("typeName").toString())){
				paramMap.put("typeName", params.get("typeName").toString());
			}
		}
		paramMap.put("orderby", "gt.update_time desc,gt.create_time desc");
		if(params.get("deptNo")!=null){
			if(!"".equals(params.get("deptNo").toString())){
				paramMap.put("deptNumber", params.get("deptNo").toString());
			}
		}

		PageHelper.startPage(pageNum, pageSize);// 设置分页的起始码以及页面大小
		List<BGoodsType> selectGoodsTypeList = bGoodsTypeService.selectGoodsTypeList(paramMap);

		PageInfo<BGoodsType> pageInfo = new PageInfo<BGoodsType>(selectGoodsTypeList);// 传入list就可以了

		return R.ok().put("goodsTypeList", pageInfo);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("basics:bgoodstype:info")
    public R info(@PathVariable("id") Integer id){
    	BGoodsType goodsType = bGoodsTypeService.selectByPrimaryKey(id);
		Map<String, Object> paramMap = new HashMap<String, Object>();

		paramMap.put("goodsType", goodsType);

		return R.ok().put("map", paramMap);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("basics:bgoodstype:save")
    public R save(@RequestBody JSONObject jsonObject){
    	BGoodsType goodsType = jsonObject.getObject("goodsType", BGoodsType.class);

		SysUserEntity user = ShiroUtils.getUserEntity();
		goodsType.setCreateUser(user.getUserId().intValue());
		int insert = bGoodsTypeService.insert(goodsType);

		if(insert>0){
			return R.ok();
		}

		return R.error();
	}

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("basics:bgoodstype:update")
    public R update(@RequestBody JSONObject jsonObject){
    	BGoodsType goodsType = jsonObject.getObject("goodsType", BGoodsType.class);
		
		if(goodsType.getId()==null){
			return R.error("需要修改的id，不能为空");
		}
		int update = bGoodsTypeService.updateByPrimaryKeySelective(goodsType);//全部更新

		if(update>0){
			return R.ok();
		}
		return R.error();
	}

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("basics:bgoodstype:delete")
    public R delete(@RequestBody Integer[] ids){


		List<Integer> idslist=new ArrayList<Integer>(Arrays.asList(ids));
		Map<String,Object> map=new HashMap<>();
		List<BGoodsType>list=bGoodsTypeService.selectIsUse(ids);
		StringBuffer stringBuffer=new StringBuffer();
		for (BGoodsType bGoodsType:list) {
			stringBuffer.append(bGoodsType.getTypeName());
			idslist.remove(bGoodsType.getId());
			stringBuffer.append(",");
		}

		ids=idslist.toArray(new Integer[idslist.size()]);
		int delete=0;
		if(ids.length>0){
			delete= bGoodsTypeService.deleteBatchIds(ids);
		}
		if(list.size()>0){
			String msg=stringBuffer.toString().substring(0,stringBuffer.toString().length() - 1);
			map.put("msg",msg+"存在使用，无法删除");
		}else{
			map.put("msg","删除成功");
		}
		if(delete==ids.length){
			return R.ok().put("data",map);
		}

    	/*int delete = bGoodsTypeService.deleteBatchIds(ids);

		if(delete>0){
			return R.ok();
		}*/

		return R.error();
    }

}
